﻿// <copyright file="MainPage.xaml.cs" company="Mooville">
//   Copyright © 2010 Roger Deetz. All rights reserved.
// </copyright>

namespace Mooville.QUno.Store
{
    using System;
    using System.Collections.Generic;
    using Windows.ApplicationModel;
    using Windows.ApplicationModel.Resources;
    using Windows.Foundation;
    using Windows.Storage;
    using Windows.Storage.Pickers;
    using Windows.UI.ApplicationSettings;
    using Windows.UI.Core;
    using Windows.UI.Popups;
    using Windows.UI.ViewManagement;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Navigation;

    public sealed partial class MainPage : Page
    {
        private Rect windowBounds;
        private Popup settingsPopup;
        private double settingsWidth = 346;

        public MainPage()
        {
            this.InitializeComponent();

            if (!DesignMode.DesignModeEnabled)
            {
                this.windowBounds = Window.Current.Bounds;
                Window.Current.SizeChanged += this.Window_SizeChanged;
                SettingsPane.GetForCurrentView().CommandsRequested += this.MainPage_CommandsRequested;
                this.buttonNew.Click += this.ButtonNew_Click;
                this.buttonOpen.Click += this.ButtonOpen_Click;
                this.buttonSave.Click += this.ButtonSave_Click;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            return;
        }

        private void OpenSettingsPopup(UserControl control)
        {
            this.settingsPopup = new Popup();
            this.settingsPopup.Closed += this.SettingsPopup_Closed;
            this.settingsPopup.IsLightDismissEnabled = true;
            this.settingsPopup.Width = this.settingsWidth;
            this.settingsPopup.Height = this.windowBounds.Height;

            Window.Current.Activated += this.Window_Activated;

            control.Width = this.settingsWidth;
            control.Height = this.windowBounds.Height;

            this.settingsPopup.Child = control;
            this.settingsPopup.SetValue(Canvas.LeftProperty, this.windowBounds.Width - this.settingsWidth);
            this.settingsPopup.SetValue(Canvas.TopProperty, 0);
            this.settingsPopup.IsOpen = true;

            return;
        }

        private bool IsUnsnapped()
        {
            return (ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap();
        }

        private void Window_SizeChanged(object sender, WindowSizeChangedEventArgs e)
        {
            this.windowBounds = Window.Current.Bounds;

            return;
        }

        private void MainPage_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            ResourceLoader resourceLoader = new ResourceLoader();
            string optionsCommandTitle = resourceLoader.GetString("SettingsOptionsTitle");
            string aboutCommandTitle = resourceLoader.GetString("SettingsAboutTitle");

            SettingsCommand optionsCommand = new SettingsCommand("optionsCommand", optionsCommandTitle, this.OptionsUICommandInvoked);
            SettingsCommand aboutCommand = new SettingsCommand("aboutCommand", aboutCommandTitle, this.AboutUICommandInvoked);
            args.Request.ApplicationCommands.Add(optionsCommand);
            args.Request.ApplicationCommands.Add(aboutCommand);

            return;
        }

        private void OptionsUICommandInvoked(IUICommand command)
        {
            this.OpenSettingsPopup(new OptionsControl());

            return;
        }

        private void AboutUICommandInvoked(IUICommand command)
        {
            this.OpenSettingsPopup(new AboutControl());

            return;
        }

        private void SettingsPopup_Closed(object sender, object e)
        {
            Window.Current.Activated -= this.Window_Activated;

            return;
        }

        private void Window_Activated(object sender, WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
            {
                this.settingsPopup.IsOpen = false;
            }

            return;
        }

        private void ButtonNew_Click(object sender, RoutedEventArgs e)
        {
            return;
        }

        private async void ButtonOpen_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsUnsnapped())
            {
                FileOpenPicker fileOpenPicker = new FileOpenPicker();
                fileOpenPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                fileOpenPicker.FileTypeFilter.Add(".quno");

                StorageFile storageFile = await fileOpenPicker.PickSingleFileAsync();

                if (storageFile != null)
                {
                    string fileName = storageFile.Name;
                }
            }

            return;
        }

        private async void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsUnsnapped())
            {
                ResourceLoader resourceLoader = new ResourceLoader();
                string savePickerFilterTitle = resourceLoader.GetString("SavePickerFilterTitle");
                string savePickerDefaultFileName = resourceLoader.GetString("SavePickerDefaultFileName");

                FileSavePicker fileSavePicker = new FileSavePicker();
                fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                fileSavePicker.FileTypeChoices.Add(savePickerFilterTitle, new List<string>() { ".quno" });
                fileSavePicker.SuggestedFileName = savePickerDefaultFileName;

                StorageFile storageFile = await fileSavePicker.PickSaveFileAsync();

                if (storageFile != null)
                {
                    string fileName = storageFile.Name;
                }
            }

            return;
        }
    }
}
